/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.app.controller;


import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.app.entity.BtcLogEntity;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.form.BtcForm;
import io.renren.modules.app.service.BtcLogService;
import io.renren.modules.app.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

/**
 * APP测试接口
 *
 * @author macy
 */
@RestController
@RequestMapping("/app")
@Api("btc 相关接口")
public class AppBtcController {

    @Autowired
    private UserService userService;

    @Autowired
    private BtcLogService btcLogService;


    @ApiOperation("下单")
    @RequestMapping(value = {"send"},method = {RequestMethod.GET,RequestMethod.POST})
    public R send(@RequestBody BtcForm form){

        UserEntity user=userService.queryByMobile("18653765897");

        //表单校验
        ValidatorUtils.validateEntity(form);

        BtcLogEntity btc=new BtcLogEntity();

        btc.setInDate(new Date());
        btc.setInPrice(new BigDecimal(form.getAmount()));
        btc.setInMoney(user.getWallet());
        btc.setDirection(form.getDirection());
        btc.setRate(form.getRate());
        btc.setAmount(new BigDecimal(form.getInAmount()));
        btc.setTxCurrency(form.getTxCurrency());
        btc.setFactorge(new BigDecimal((form.getInAmount()*10)/100));

        if(user.getWallet().doubleValue()<(form.getInAmount()+btc.getFactorge().doubleValue())){
            return R.error("余额不足");
        }

        user.setWallet(user.getWallet().subtract(new BigDecimal(form.getInAmount())).subtract(btc.getFactorge()));
        if(!userService.updateById(user)){
            return R.error("下单保存失败");
        }
        if(!btcLogService.save(btc)){
            return R.error("下单保存失败");
        }

        return R.ok().put("data",btc);
    }



    @ApiOperation("平仓")
    @RequestMapping(value = {"flat"},method = {RequestMethod.GET,RequestMethod.POST})
    public R flat(@RequestParam Integer id,@RequestParam Double amount){

        BtcLogEntity btc=btcLogService.queryById(id);
        if(null==btc){
            return R.error("查无仓位");
        }

        if(btc.getOutDate()!=null){
            return R.error("已平仓");
        }

        UserEntity user=userService.queryByMobile("18653765897");


        BigDecimal xc=new BigDecimal(amount).subtract(btc.getInPrice());
        boolean add=true;

        if(btc.getDirection().equals("buy")){
            if(xc.doubleValue()<0){
                add=false;
            }
        }else{
            if(xc.doubleValue()>0){
                add=false;
            }
        }

        BigDecimal bc=xc.abs().divide(new BigDecimal((btc.getRate()==50)?BtcLogEntity.MAX:(BtcLogEntity.MAX*2)),4, RoundingMode.HALF_UP);
        if(bc.doubleValue()>1){
            bc=new BigDecimal(1);
        }


        BigDecimal res=bc.multiply(btc.getAmount());
        if(add){
            user.setWallet(user.getWallet().add(res).add(btc.getAmount()));
        }else{
            user.setWallet(user.getWallet().add(btc.getAmount().subtract(res)));
        }


        if(!userService.updateById(user)){
            return R.error("下单保存失败");
        }



        btc.setOutDate(new Date());
        btc.setOutPrice(new BigDecimal(amount));
        btc.setOutMoney(user.getWallet());



        if(!btcLogService.updateById(btc)){
            return R.error("下单保存失败");
        }

        return R.ok().put("data",btc);
    }

}
